package com.ruoyi.oos.web.controller.overrun;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oos.duty.domain.Duty;
import com.ruoyi.oos.duty.service.IDutyService;
import com.ruoyi.oos.overrun.domain.OverrunRecord;
import com.ruoyi.oos.overrun.service.IOverrunRecordService;
import com.ruoyi.oos.tag.domain.TagConfig;
import com.ruoyi.oos.tag.domain.TagLimitdtl;
import com.ruoyi.oos.tag.mapper.TagLimitdtlMapper;
import com.ruoyi.oos.tag.service.ITagConfigService;
import com.ruoyi.oos.tag.service.ITagGroupService;
import com.ruoyi.oos.tag.service.impl.PiUtilBean;
import com.ruoyi.web.controller.tag.ChartBean;

/**
 * 超限记录Controller
 * 
 * @author bulang
 * @date 2020-05-13
 */
@Controller
@RequestMapping("/overrun/record")
public class OverrunRecordController extends BaseController
{
    private String prefix = "overrun/record";

    @Autowired
    private IOverrunRecordService overrunRecordService;

    @Autowired
    private ITagConfigService tagConfigService;
    
    @Autowired
    private ITagGroupService tagGroupService;

    @Autowired
    private TagLimitdtlMapper limitMapper;
    
    @Autowired
    private IDutyService dutyService;
    
    @RequiresPermissions("overrun:record:view")
    @GetMapping()
    public String record(ModelMap map)
    {
	    	OverrunRecord overrunRecord = new OverrunRecord();
	    	overrunRecord.setStartDate(DateUtils.getFirstDayOfMonth());
	    	overrunRecord.setEndDate(DateUtils.getDate());
	  		
	  		map.put("data", overrunRecord);
  		
        return prefix + "/record";
    }

        /**
     * 查询超限记录列表
     */
    @RequiresPermissions("overrun:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OverrunRecord overrunRecord)
    {
        // 时间查询条件处理
        String startDate = overrunRecord.getStartDate();
        if (StringUtils.isNotEmpty(startDate)) {
        	overrunRecord.setStartDate(startDate + " 00:00:00");
        }
        String endDate = overrunRecord.getEndDate();
        if (StringUtils.isNotEmpty(endDate)) {
        	overrunRecord.setEndDate(endDate + " 23:59:59");
        }
        
        if (overrunRecord.getGroupid() != null) {
        	List<Long> list = tagGroupService.selectSubTagGroupById(overrunRecord.getGroupid());
        	overrunRecord.setGroupIds(list);
        }
        
        startPage();
        
        List<OverrunRecord> list = overrunRecordService.selectOverrunRecordList(overrunRecord);
        return getDataTable(list);
    }

    /**
     * 跳转到超限记录查询页面
     * @param overrunRecord
     * @param map
     * @return
     */
    @RequiresPermissions("overrun:tagcount:view")
    @GetMapping("/toDetail")
    public String toDetail(OverrunRecord overrunRecord, ModelMap map)
    {
    		if (overrunRecord.getDutyid() == null) {
    			Duty duty = new Duty();
    			duty.setStatus("1");
    			List<Duty> list = dutyService.selectDutyList(duty);
    			if (list != null && list.size() > 0) {
    				overrunRecord.setDutyid(list.get(0).getId());
    			}
    		}
    		map.put("data", overrunRecord);
        return prefix + "/record";
    }
    
    /**
     * 图表显示超限记录
     * @param id
     * @param mmap
     * @return
     * @throws Exception
     */
    @GetMapping("/echart/{id}")
    public String echart(@PathVariable("id") Long id, ModelMap map) throws Exception
    {
    	OverrunRecord record = overrunRecordService.selectOverrunRecordById(id);//查询超限记录
    	String starttime = DateUtils.addDateStr(record.getStarttime(), Calendar.MINUTE, -10);
    	String endtime = DateUtils.addDateStr(record.getEndtime(), Calendar.MINUTE, 10);
    	TagConfig tag = tagConfigService.selectTagConfigById(record.getTagid());//查询测点
    	PiUtilBean brean = tagConfigService.getChartData(tag.getPitagname(), starttime, endtime);//查询测点曲线数据
      List<Float> list =brean.getRetrunList();
      map.put("tagConfig", tag.getTagname()+"("+DateUtils.dateTime1(record.getStarttime())+"至"+DateUtils.dateTime1(record.getEndtime())+")");
      map.put("list", list);
      map.put("xList",brean.getReturntimes());
        
        TagLimitdtl limtitl = new TagLimitdtl();
        limtitl.setConfigid(tag.getId());
       List<TagLimitdtl> limittlList =  limitMapper.selectTagLimitdtlList(limtitl);
       // Float i = 2f;
       if(limittlList!= null && list.size()>0) {
    	   List<ChartBean> limitList = new ArrayList<ChartBean>();
    	   for (TagLimitdtl tagLimitdtl : limittlList) {
    		   ChartBean bean = new ChartBean();
    		   Float limitvalue=  tagLimitdtl.getLimitval();
    		   if(limitvalue == null) {
    			   limitvalue =0f;
    		   }
    		   bean.setyAxis(limitvalue);
    		   limitList.add(bean);
    	   }
    	   map.put("limitList", limitList); 
       }
       
       return prefix + "/echartData";
    }
    
    /**
     * 导出超限记录列表
     */
    @RequiresPermissions("overrun:record:export")
    @Log(title = "超限记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OverrunRecord overrunRecord)
    {
        List<OverrunRecord> list = overrunRecordService.selectOverrunRecordList(overrunRecord);
        ExcelUtil<OverrunRecord> util = new ExcelUtil<OverrunRecord>(OverrunRecord.class);
        return util.exportExcel(list, "超限记录");
    }
}
